﻿using Se.Model.Host;
using Se.Model.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Se.Data;

namespace Se.Model.GameModel.Character
{
    /// <summary>
    /// 游戏角色
    /// </summary>
    public class Player : DataEntityBase
    {
        public Player(DBSaveType saveType = DBSaveType.Insert)
            : base(saveType, DBLoadType.MemoryThenDB)
        { }

        /// <summary>
        /// 账户
        /// </summary>
        public Account Account { get; private set; }

        /// <summary>
        /// 账户ID
        /// </summary>
        [EntityField]
        public long AccountID { get; set; }

        /// <summary>
        /// 账户名称
        /// </summary>
        [EntityField]
        public string Name { get; set; }

        /// <summary>
        /// 性别
        /// </summary>
        [EntityField]
        public PlayerGender Gender { get; set; }

        /// <summary>
        /// 头像ID
        /// </summary>
        [EntityField]
        public short HeaderID { get; set; }

        /// <summary>
        /// 在线状态
        /// </summary>
        [EntityField]
        public OnlineState OnlineState { get; set; }

        /// <summary>
        /// 玩家等级
        /// </summary>
        [EntityField]
        public int Level { get; set; }

        /// <summary>
        /// 玩家创建时间
        /// </summary>
        [EntityField]
        public DateTime RoleCreateTime { get; set; }

        /// <summary>
        /// 上次登录时间
        /// </summary>
        [EntityField]
        public DateTime LastLoginTime { get; set; }

        /// <summary>
        /// Session
        /// </summary>
        public SocketSession Session { get; set; }


        public void SetAccount(Account account)
        {
            this.Account = account;
            this.AccountID = account.ID;
        }

        public override void Save<T>()
        {
            this.UpdateSimPlayer();

            this.Simple.Save<PlayerSim>();
            base.Save<T>();
        }

        /// <summary>
        /// 登出处理
        /// </summary>
        public void OnLogout()
        { }

        /// <summary>
        /// 登录处理
        /// </summary>
        public void OnLogin()
        {
            this.LastLoginTime = DateTime.Now;
        }

        public PlayerSim Simple { get; set; }

        /// <summary>
        /// SimplePlayer
        /// </summary>
        /// <returns></returns>
        private void UpdateSimPlayer()
        {
            if (this.Simple == null)
                this.Simple = new PlayerSim();
            this.Simple.ID = this.ID;
            this.Simple.AccountID = this.AccountID;
            this.Simple.Name = this.Name;            
        }
    }

    public class PlayerSim : DataEntityBase
    {
        public PlayerSim(DBSaveType saveType = DBSaveType.Insert) : base(saveType, DBLoadType.MemoryAlways, false) { }

        /// <summary>
        /// 账户ID
        /// </summary>
        [EntityField]
        public long AccountID { get; set; }

        /// <summary>
        /// 账户名称
        /// </summary>
        [EntityField]
        public string Name { get; set; }
    }
}
